/*
* sop组件与工序关联表模块
*/
const express = require('express');
const router = express.Router();
// 引入数据库
const db = require("../../config/db");
// 引入时间包
const moment = require("moment");
// 引入方法库
const { toCamelCase, toUnderlineCase, verifyToken, recursionGenerateTree } = require("../../config/utils");
// 引入日志
const log = require("../log");

// 修改*sop组件与工序关联表*
router.put("/updateComponentProcess", (req, res) => {
    console.log("=========== 修改*sop组件与工序关联表*接口============");
    // 获取参数
    let { oldId, parentId, deptId, orderNum } = req.body;
    // 截取id字段
    parentId = parentId.split("-")[1];
    deptId = deptId.split("-")[1];
    oldId = oldId ? oldId.split("-")[1] : null;
    /// 修改数据库
    // 判断当前组件是否已存在该工序
    let sql_check = `select * from sop_components_sop_process where component_id = '${parentId}' and process_id = '${deptId}'`;
    db.query("修改*sop组件与工序关联表*判断当前组件是否已存在该工序", sql_check, (err, sql_check_result) => {
        if (err) {
            res.send({ code: 500, msg: "修改*sop组件与工序关联表*判断当前组件是否已存在该工序失败", data: err });
            return;
        }
        if (sql_check_result.length > 0) {
            // 判断是否是同一个工序，如果是，则修改排序
            if (sql_check_result[0].process_id == oldId) {
                // 修改排序
                let sql = `update sop_components_sop_process set order_num = '${orderNum}' where component_id = '${parentId}' and process_id = '${deptId}'`;
                db.query("修改*sop组件与工序关联表排序*", sql, (err1, result) => {
                    if (err1) {
                        res.send({ code: 500, msg: "修改*sop组件与工序关联表排序*失败", data: err1 });
                        return;
                    }
                    if (result.affectedRows > 0) {
                        res.send({ code: 200, msg: "修改*sop组件与工序关联表排序*成功" });
                    } else {
                        res.send({ code: 500, msg: "修改*sop组件与工序关联表排序*失败" });
                    }
                })
            } else {
                res.send({ code: 500, msg: "当前组件已存在该工序，无法修改" });
            }

        } else {
            // 判断是否第一次添加工序
            if (oldId) {
                // 删除当前组件与工序的关联
                let sql_delete = `delete from sop_components_sop_process where component_id = '${parentId}' and process_id = '${oldId}'`;
                db.query("修改*sop组件与工序关联表*删除当前组件与工序的关联", sql_delete, (err1, result) => {
                    if (err1) {
                        res.send({ code: 500, msg: "修改*sop组件与工序关联表*删除当前组件与工序的关联失败", data: err1 });
                        return;
                    }
                    if (result.affectedRows > 0) {
                        // 添加当前组件与工序的关联
                        let sql_add = `insert into sop_components_sop_process (component_id, process_id, order_num) values ('${parentId}', '${deptId}', '${orderNum}')`;
                        db.query("修改*sop组件与工序关联表*添加当前组件与工序的关联", sql_add, (err2, result2) => {
                            if (err2) {
                                res.send({ code: 500, msg: "修改*sop组件与工序关联表*添加当前组件与工序的关联失败", data: err2 });
                                return;
                            }
                            if (result2.affectedRows > 0) {
                                res.send({ code: 200, msg: "修改*sop组件与工序关联表*成功" });
                            } else {
                                res.send({ code: 500, msg: "修改*sop组件与工序关联表*失败" });
                            }
                        })
                    } else {
                        res.send({ code: 500, msg: "修改*sop组件与工序关联表*失败" })
                    }
                })
            } else {
                // 添加当前组件与工序的关联
                let sql_add = `insert into sop_components_sop_process (component_id, process_id, order_num) values ('${parentId}', '${deptId}', '${orderNum}')`;
                db.query("添加当前组件与工序的关联", sql_add, (err2, result2) => {
                    if (err2) {
                        res.send({ code: 500, msg: "添加当前组件与工序的关联失败", data: err2 });
                        return;
                    }
                    if (result2.affectedRows > 0) {
                        res.send({ code: 200, msg: "添加当前组件与工序的关联成功" });
                    } else {
                        res.send({ code: 500, msg: "添加当前组件与工序的关联失败" });
                    }
                })
            }

        }
    })
})

// 删除*sop*组件与工序关联表*
router.delete("/deleteComponentProcess/:componentId/:processId", (req, res) => {
    console.log("=========== 删除*sop*组件与工序关联表*接口============");
    // 获取参数
    let { componentId, processId } = req.params;
    // 删除组件与工序关联表数据库
    let sql = `delete from sop_components_sop_process where process_id = '${processId}' and component_id = '${componentId}'`;
    db.query("删除*sop*组件与工序关联表*", sql, (err, result) => {
        if (err) {
            res.send({ code: 500, msg: "删除*sop*组件与工序关联表*失败", data: err });
            return;
        }
        if (result.affectedRows > 0) {
            res.send({ code: 200, msg: "删除*sop*组件与工序关联表*成功" });
        } else {
            res.send({ code: 500, msg: "删除*sop*组件与工序关联表*失败" });
        }

    })
})

module.exports = router;